package com.wushi.lenovo.asia5b.app_ui.profile.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.mvp.base.BaseAppCompatActivity;
import com.mvp.common.GlobalValue;
import com.mvp.common.LocalManageUtil;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_ui.other.activity.MainActivity;

import java.util.Locale;

import butterknife.BindView;

import static timber.log.Timber.d;

/**
 * Created by zhangyuncai on 2018/12/11.
 */
public class SetLanguageActivity extends BaseAppCompatActivity {
    @BindView(R.id.rb_setlanguage_china)
    RadioButton rb_setlanguage_china;
    @BindView(R.id.rb_setlanguage_english)
    RadioButton rb_setlanguage_english;
    @BindView(R.id.rg_setlanguage_vessel)
    RadioGroup rg_setlanguage_vessel;
    private boolean isEnglish = false;//默认中文

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setlanguage;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();

        initData();

        initEvent();
    }

    @Override
    protected void initView() {

        Locale locale = LocalManageUtil.getAppLanguageSelect();
        isEnglish = locale == Locale.ENGLISH;
        rb_setlanguage_china.setChecked(!isEnglish);
        rb_setlanguage_english.setChecked(isEnglish);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        rg_setlanguage_vessel.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                isEnglish = checkedId == R.id.rb_setlanguage_english;
                submit();
            }
        });

    }

    private void submit() {
        Locale preLocale = isEnglish ? Locale.ENGLISH : Locale.CHINA;//准备设置的语言
        Locale locale = LocalManageUtil.getAppLanguageSelect();
        d("--->>>>>>>>---APP语言:" + locale.getLanguage());
        d("--->>>>>>>>---准备设置APP语言:" + preLocale.getLanguage());
        if (locale == preLocale) {//只有当设置的语言不等于APP本来的语言时,才更改语言
            return;
        }
        LocalManageUtil.setAppLanguageSelect(isEnglish);
        LocalManageUtil.getInstance().setConfiguration();
        GlobalValue.saveFirstSetLanguage(false);

        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }
}
